package com.ro.model.blog;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Redis;

import java.util.logging.Level;
import java.util.logging.Logger;

public class AuthStateInterceptor implements Interceptor {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void intercept(Invocation inv) {
        String sessionAuthToken = "";
        Controller c = inv.getController();
        String authToken = c.getCookie("ISL");
        try {
            sessionAuthToken = Redis.use("ec").get("authToken");
            if(sessionAuthToken == null){
                sessionAuthToken = "";
            }
        } catch (Exception e) {
            logger.log(Level.INFO, "读缓存有问题,可能没初始化!");
        }
        if (sessionAuthToken.equals(authToken)) {
            c.setAttr("ISL", 1);
            inv.invoke();
        } else {
            c.setAttr("ISL", 0);
            inv.invoke();
        }
    }
}
